package com.maiworld.seller.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maiworld.mapper.TbCoachMapper;
import com.maiworld.mapper.TbConsumeMapper;
import com.maiworld.mapper.TbReleaseNewsMapper;
import com.maiworld.pojo.TbCoach;
import com.maiworld.pojo.TbConsume;
import com.maiworld.pojo.TbReleaseNews;
import com.maiworld.seller.service.ReleaseNawsService;

@Service
public class ReleaseNawsServiceImpl implements ReleaseNawsService {

	@Autowired
	private TbCoachMapper tbCoachMapper;
	
	@Autowired
	private TbReleaseNewsMapper tbReleaseNewsMapper;
	
	@Autowired
	private TbConsumeMapper tbConsumeMapper;
	
	
	//获取某个教练发布的还没有过期的所有任务信息 cid:代表教练的主键 这里一个教练在某一个阶段只能教科目
	@Override
	public Map<String, Object> findComment(Long id, Long cid, Long sid,String status, String openid, String date,String subject){
		Map<String, Object> result = new HashMap<String, Object>();
		
		//获取教练信息
		TbCoach coach = new TbCoach();
		coach= tbCoachMapper.selectByPrimaryKey(cid);				
		result.put("coach_info", coach);
		
		TbConsume sume = new TbConsume();
		sume.setStatus(status);
		sume.setTypelog("1");
		
		List<TbConsume> a = tbConsumeMapper.selectByConsume(sume);
		result.put("integral", a.get(0).getNumberCoins());
		result.put("flag", a.get(0).getFlag());
		/*TbConsume a = tbConsumeMapper.selectByPrimaryKey(id);
		String c = a.getNumberCoins();
		result.put("integral", c);*/
		//教练发布信息表
		List<TbReleaseNews> array = new ArrayList<TbReleaseNews>();
		TbReleaseNews example = new TbReleaseNews();
		example.setCid(cid);		
		example.setDate(date);
		example.setStatus(status);
		example.setSubject(subject);
		array =(List<TbReleaseNews>) tbReleaseNewsMapper.selectByExampleId(example);
		
		Map<String, Object> arrItem= new HashMap<String, Object>();
		result.put("arrItem", date);
		if(array.isEmpty()){
			arrItem.put("arr", null);
		}else {
			arrItem.put("arr", array);
		}
		result.put("arr", arrItem);
		result.put("statusCode", 200);
		result.put("message", "成功");
		return result;	
		/*List<TbReleaseNews> B = new ArrayList<TbReleaseNews>();
		TbReleaseNews a = tbReleaseNews;
		for (int i = 0; i < coach.size(); i++) {
			for (int j = 0; j < array.size(); j++) {
				if(array.get(j).getCid().equals(coach.get(i).getCid())){
					a.setId(array.get(j).getId());
					a.setCid(array.get(j).getCid());
					a.setPeriod(array.get(j).getPeriod());
					a.setNumber(array.get(j).getNumber());
					a.setPeopleNum(array.get(j).getPeopleNum());
					a.setSubject(array.get(j).getSubject());
					a.setVehicleType(array.get(j).getVehicleType());
					a.setTime(array.get(j).getTime());
					B.add(a);
				}
			}
		}*/	
	}

	//教练发布工单

	@Override
	public Map<String, Object> findCoachRelease(@RequestBody String parmes) {
		
		Map<String, Object> result = new HashMap<String, Object>();
		//数据处理
		JSONObject jsonobjectF = JSONObject.parseObject(parmes);
		
		JSONObject jsonobject = jsonobjectF.getJSONObject("params");
		
		String session_key =  jsonobject.getString("session_key");
		
		String openid =  jsonobject.getString("openid");
		
		String status =  jsonobject.getString("status");
		
		JSONArray results = jsonobject.getJSONArray("results");
		
		
		for (int i = 0; i < results.size(); i++) {
			
			JSONObject plans = results.getJSONObject(i);
			TbReleaseNews rele = new TbReleaseNews();
			rele.setCid(Long.parseLong(plans.getString("cid")));
			rele.setCname(plans.getString("cname"));
			rele.setPeriod(plans.getString("period"));
			rele.setTime(plans.getString("time"));
			rele.setDate(plans.getString("date"));
			rele.setNumber(Long.parseLong(plans.getString("number")));
			rele.setPeopleNum(Long.parseLong(plans.getString("people_num")));
			rele.setSubject(plans.getString("subject"));
			rele.setVehicleType(plans.getString("vehicle_type"));
			rele.setStatus(plans.getString("status"));
			
			tbReleaseNewsMapper.insertSelective(rele);					
		}
		result.put("statusCode", 200);
		result.put("message", "成功");
		/*TbReleaseNews rele = new TbReleaseNews();
		rele.setCid(Long.parseLong(plans.getString("cid")));
		rele.setCname(plans.getString("cname"));
		rele.setPeriod(plans.getString("period"));
		rele.setTime(plans.getString("time"));
		rele.setDate(plans.getString("date"));
		rele.setNumber(Long.parseLong(plans.getString("number")));
		rele.setPeopleNum(Long.parseLong(plans.getString("peopleNum")));
		rele.setSubject(plans.getString("subject"));
		rele.setVehicleType(plans.getString("vehicleType"));
		rele.setStatus(plans.getString("status"));
		tbReleaseNewsMapper.insertSelective(rele);
		
		result.put("statusCode", 200);
		result.put("message", "成功");*/
		return result;
	}

	//重新发布工单
	@Override
	public Map<String, Object> deleteRele(@RequestBody String parmes) {
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		//数据处理
		JSONObject jsonobjectF = JSONObject.parseObject(parmes);
		
		JSONObject jsonobject = jsonobjectF.getJSONObject("params");
		
		String session_key =  jsonobject.getString("session_key");
		
		Long cid =  jsonobject.getLong("cid");
		
		String date =  jsonobject.getString("date");
		
		String openid =  jsonobject.getString("openid");
		
		String status =  jsonobject.getString("status");
		
		JSONArray results = jsonobject.getJSONArray("results");
		

		TbReleaseNews news = new TbReleaseNews();
		news.setCid(cid);
		news.setDate(date);
		news.setStatus(status);
		tbReleaseNewsMapper.deleteByPrimaryDate(news);
		
		for (int i = 0; i < results.size(); i++) {

			JSONObject plans = results.getJSONObject(i);
			TbReleaseNews rele = new TbReleaseNews();
			rele.setCid(Long.parseLong(plans.getString("cid")));
			rele.setCname(plans.getString("cname"));
			rele.setPeriod(plans.getString("period"));
			rele.setTime(plans.getString("time"));
			rele.setDate(plans.getString("date"));
			rele.setNumber(Long.parseLong(plans.getString("number")));
			rele.setPeopleNum(Long.parseLong(plans.getString("people_num")));
			rele.setSubject(plans.getString("subject"));
			rele.setVehicleType(plans.getString("vehicle_type"));
			rele.setStatus(plans.getString("status"));
			
			tbReleaseNewsMapper.insertSelective(rele);			
		}
		
		result.put("statusCode", 200);
		result.put("message", "成功");
		return result;
		
		
		
		
	}

	//获取教练的任务工单
	@Override
	public Map<String, Object> getplan(Long cid, String date, String status, String openid) {
		
		Map<String, Object> result = new HashMap<String, Object>();
		TbReleaseNews a = new TbReleaseNews();
		a.setCid(cid);
		a.setStatus(status);
		a.setDate(date);
		
		List<TbReleaseNews> b =tbReleaseNewsMapper.selectByExampleId(a);
		
		result.put("releaseNews", b);
		result.put("statusCode", 200);
		result.put("message", "成功");
		return result;
	}
	
}
